﻿using Dtos;
using IBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IDAL;

namespace BLL
{
    internal class PositionsBll : IPositionsBll
    {
        private IPositionsDal _dal;
        public PositionsBll(IPositionsDal dal)
        {
            _dal = dal;
        }
        public async Task<int> AddPositionsAsync(string title)
        {
            return await _dal.AddAsync(new Model.Positions { Title = title});
        }

        public async Task<int> DeletePositionsAsync(Guid id)
        {
            var data = await _dal.GetById(id);
            return data != null ? await _dal.DeleteAsync(data) : 0;
        }

        public async Task<List<PositionsDto>> GetAll()
        {
            var data = await _dal.GetAll();
            return data.Select(p => new PositionsDto 
            {
                Id = p.Id,
                Title = p.Title,
                UpdateTime = DateTime.Now,
                DeleteId = p.DeleteId,
            }).ToList();
        }

        public Task<PositionsDto> GetPositionsByIdAsync(Guid id)
        {
            throw new NotImplementedException();
        }

        public Task<List<PositionsDto>> GetPositionsByNameAsync(string title)
        {
            throw new NotImplementedException();
        }

        public async Task<int> UpdatePositionsAsync(Guid id, string title, DateTime updateTime)
        {
            var data = await _dal.GetById(id);
            if (data == null)
                return 0;
            data.Title = title;
            data.UpdateTime = updateTime;
            return await _dal.UpdateAsync(data);
        }
    }
}
